package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.Statement;
import org.light.domain.StatementList;
import org.light.utils.WriteableUtil;

public class DateTimeAndDateGoGenerator extends Generator{
	protected boolean containsAuth = false;
	protected Domain sampleDomain;
	public boolean isContainsAuth() {
		return containsAuth;
	}

	public void setContainsAuth(boolean containsAuth) {
		this.containsAuth = containsAuth;
	}

	public DateTimeAndDateGoGenerator(Domain sampleDomain){
		super();
		super.fileName = "DateTimeAndDate.go";
		super.standardName = "DateTimeAndDate";
		this.sampleDomain = sampleDomain;
	}

	@Override
	public StatementList generateStatementList() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package "+this.sampleDomain.getDomainSuffix()));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import ("));
		sList.add(new Statement(4000L,1,"\"time\""));
		sList.add(new Statement(4500L,1,"\"strings\""));
		sList.add(new Statement(5000L,1,"\"database/sql\""));
		sList.add(new Statement(6000L,1,"\"encoding/json\""));
		sList.add(new Statement(7000L,0,")"));
		sList.add(new Statement(8000L,0,""));
		sList.add(new Statement(9000L,0,"type DateTime sql.NullTime"));
		sList.add(new Statement(10000L,0,"type Date sql.NullTime"));
		sList.add(new Statement(11000L,0,""));
		sList.add(new Statement(12000L,0,"const ("));
		sList.add(new Statement(13000L,1,"DateTimeFormart = \"2006-01-02 15:04:05\""));
		sList.add(new Statement(14000L,1,"DateFormart = \"2006-01-02\""));
		sList.add(new Statement(15000L,0,")"));
		sList.add(new Statement(16000L,0,""));
		sList.add(new Statement(17000L,0,"func (t *DateTime) UnmarshalJSON(data []byte) (err error) {"));
		sList.add(new Statement(18000L,1,"if data == nil || strings.TrimSpace(string(data)) == \"\\\"\\\"\" {"));
		sList.add(new Statement(19000L,2,"*t = DateTime{"));
		sList.add(new Statement(20000L,3,"Time: time.Time{},"));
		sList.add(new Statement(21000L,3,"Valid: false,"));
		sList.add(new Statement(22000L,2,"}"));
		sList.add(new Statement(23000L,2,"return"));
		sList.add(new Statement(24000L,1,"}"));
		sList.add(new Statement(25000L,1,"now, _ := time.ParseInLocation(`\"`+DateTimeFormart+`\"`, string(data), time.Local)"));
		sList.add(new Statement(26000L,1,"*t = DateTime{"));
		sList.add(new Statement(27000L,2,"Time: now,"));
		sList.add(new Statement(28000L,2,"Valid: true,"));
		sList.add(new Statement(29000L,1,"}"));
		sList.add(new Statement(30000L,1,"return"));
		sList.add(new Statement(31000L,0,"}"));
		sList.add(new Statement(32000L,0,""));
		sList.add(new Statement(33000L,0,""));
		sList.add(new Statement(34000L,0,"func (t DateTime) MarshalJSON() ([]byte, error) {"));
		sList.add(new Statement(35000L,1,"if t.Valid {"));
		sList.add(new Statement(36000L,2,"b := make([]byte, 0, len(DateTimeFormart)+2)"));
		sList.add(new Statement(37000L,2,"b = append(b, '\"')"));
		sList.add(new Statement(38000L,2,"b = time.Time(t.Time).AppendFormat(b, DateTimeFormart)"));
		sList.add(new Statement(39000L,2,"b = append(b, '\"')"));
		sList.add(new Statement(40000L,2,"return b, nil"));
		sList.add(new Statement(41000L,1,"} else {"));
		sList.add(new Statement(42000L,2,"return json.Marshal(\"\")"));
		sList.add(new Statement(43000L,1,"}"));
		sList.add(new Statement(44000L,0,"}"));
		sList.add(new Statement(45000L,0,""));
		sList.add(new Statement(46000L,0,"func (t DateTime) String() string {"));
		sList.add(new Statement(47000L,1,"if t.Valid {"));
		sList.add(new Statement(48000L,2,"return time.Time(t.Time).Format(DateTimeFormart)"));
		sList.add(new Statement(49000L,1,"} else {"));
		sList.add(new Statement(50000L,2,"return \"\""));
		sList.add(new Statement(51000L,1,"}"));
		sList.add(new Statement(52000L,0,"}"));
		sList.add(new Statement(53000L,0,""));
		sList.add(new Statement(54000L,0,"func (t *Date) UnmarshalJSON(data []byte) (err error) {"));
		sList.add(new Statement(55000L,1,"if data == nil || strings.TrimSpace(string(data)) == \"\\\"\\\"\" {"));
		sList.add(new Statement(56000L,2,"*t = Date{"));
		sList.add(new Statement(57000L,3,"Time: time.Time{},"));
		sList.add(new Statement(58000L,3,"Valid: false,"));
		sList.add(new Statement(59000L,2,"}"));
		sList.add(new Statement(60000L,2,"return"));
		sList.add(new Statement(61000L,1,"}"));
		sList.add(new Statement(62000L,1,"now, err := time.ParseInLocation(`\"`+DateFormart+`\"`, string(data), time.Local)"));
		sList.add(new Statement(63000L,1,"*t = Date{"));
		sList.add(new Statement(64000L,2,"Time: now,"));
		sList.add(new Statement(65000L,2,"Valid: true,"));
		sList.add(new Statement(66000L,1,"}"));
		sList.add(new Statement(67000L,1,"return"));
		sList.add(new Statement(68000L,0,"}"));
		sList.add(new Statement(69000L,0,""));
		sList.add(new Statement(70000L,0,"func (t Date) MarshalJSON() ([]byte, error) {"));
		sList.add(new Statement(71000L,1,"if t.Valid {"));
		sList.add(new Statement(72000L,2,"b := make([]byte, 0, len(DateFormart)+2)"));
		sList.add(new Statement(73000L,2,"b = append(b, '\"')"));
		sList.add(new Statement(74000L,2,"b = time.Time(t.Time).AppendFormat(b, DateFormart)"));
		sList.add(new Statement(75000L,2,"b = append(b, '\"')"));
		sList.add(new Statement(76000L,2,"return b, nil"));
		sList.add(new Statement(77000L,1,"} else {"));
		sList.add(new Statement(78000L,2,"return json.Marshal(\"\")"));
		sList.add(new Statement(79000L,1,"}"));
		sList.add(new Statement(80000L,0,"}"));
		sList.add(new Statement(81000L,0,""));
		sList.add(new Statement(82000L,0,"func (t Date) String() string {"));
		sList.add(new Statement(83000L,1,"if t.Valid {"));
		sList.add(new Statement(84000L,2,"return time.Time(t.Time).Format(DateFormart)"));
		sList.add(new Statement(85000L,1,"} else {"));
		sList.add(new Statement(86000L,2,"return  \"\""));
		sList.add(new Statement(87000L,1,"}"));
		sList.add(new Statement(88000L,0,"}"));
		sList.add(new Statement(89000L,0,""));
		sList.add(new Statement(90000L,0,"func (t Date) SqlString() sql.NullString {"));
		sList.add(new Statement(91000L,1,"if t.Valid {"));
		sList.add(new Statement(92000L,2,"return sql.NullString{String: time.Time(t.Time).Format(DateFormart), Valid: true}"));
		sList.add(new Statement(93000L,1,"} else {"));
		sList.add(new Statement(94000L,2,"return sql.NullString{String: \"null\", Valid: false}"));
		sList.add(new Statement(95000L,1,"}"));
		sList.add(new Statement(96000L,0,"}"));
		sList.add(new Statement(97000L,0,""));
		sList.add(new Statement(98000L,0,"func (t DateTime) SqlString() sql.NullString {"));
		sList.add(new Statement(99000L,1,"if t.Valid {"));
		sList.add(new Statement(100000L,2,"return sql.NullString{String: time.Time(t.Time).Format(DateTimeFormart), Valid: true}"));
		sList.add(new Statement(101000L,1,"} else {"));
		sList.add(new Statement(102000L,2,"return sql.NullString{String: \"null\", Valid: false}"));
		sList.add(new Statement(103000L,1,"}"));
		sList.add(new Statement(104000L,0,"}"));
		
		sList.add(new Statement(105000L,0,""));
		sList.add(new Statement(106000L,0,"func (t Date) ToTime() time.Time {"));
		sList.add(new Statement(107000L,0,"if t.Valid {"));
		sList.add(new Statement(108000L,2,"return time.Time(t.Time)"));
		sList.add(new Statement(109000L,1,"} else {"));
		sList.add(new Statement(110000L,2,"return time.Time{}"));
		sList.add(new Statement(111000L,1,"}"));
		sList.add(new Statement(112000L,0,"}"));
		sList.add(new Statement(113000L,0,""));
		sList.add(new Statement(114000L,0,"func (t DateTime) ToTime() time.Time {"));
		sList.add(new Statement(115000L,1,"if t.Valid {"));
		sList.add(new Statement(116000L,2,"return time.Time(t.Time)"));
		sList.add(new Statement(117000L,1,"} else {"));
		sList.add(new Statement(118000L,2,"return time.Time{}"));
		sList.add(new Statement(119000L,1,"}"));
		sList.add(new Statement(120000L,0,"}"));



		return WriteableUtil.merge(sList);
	}

	public Domain getSampleDomain() {
		return sampleDomain;
	}

	public void setSampleDomain(Domain sampleDomain) {
		this.sampleDomain = sampleDomain;
	}

}
